package com.quzi.kettle.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.quzi.kettle.R;

/**
 */
public class TitleBarView extends LinearLayout {

	private View mainView;
	private Context mContext;

	private TextView mTitle;
	private ImageView mBack;
	private ImageView rightIcon;
	private TextView mConnException;
	private TextView mTextRight;

	private TitleBarHandler handler;

	public TitleBarView(Context context) {
		this(context, null, 0);
	}

	public TitleBarView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public TitleBarView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);

		this.mContext = context;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mainView = inflater.inflate(R.layout.title_view, this);

		mTitle = (TextView) mainView.findViewById(R.id.titleview_title);
		mTextRight = (TextView) mainView.findViewById(R.id.titleview_right);
		mTextRight.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (handler != null) {
					handler.ClickRight();
				}
			}
		});
		rightIcon = (ImageView) mainView.findViewById(R.id.right_icon);
		rightIcon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (handler != null) {
					handler.ClickRight();
				}
			}
		});
		mBack = (ImageView) mainView.findViewById(R.id.titleview_iv_back);
		mBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (handler != null) {
					handler.ClickLeft();
				} else {
					((Activity) mContext).finish();
				}
			}
		});

		mConnException = (TextView) mainView
				.findViewById(R.id.titleview_tv_conn_exception);
		mConnException.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO 添加网络链接异常的处理
			}
		});

	}

	public void setRightIconInVisiable(boolean flag) {
		if (flag) {
			rightIcon.setVisibility(View.INVISIBLE);
		} else {
			rightIcon.setVisibility(View.VISIBLE);
		}

	}

	/**
	 * 显示没有网络
	 * 
	 * @param isShow
	 */
	public void showConnExceptionView(boolean isShow) {
		if (isShow) {
			mConnException.startAnimation(AnimationUtils.loadAnimation(
					mContext, android.R.anim.fade_in));
			mConnException.setVisibility(View.VISIBLE);
		} else {
			mConnException.startAnimation(AnimationUtils.loadAnimation(
					mContext, android.R.anim.fade_out));
			mConnException.setVisibility(View.GONE);
		}
	}

	/**
	 * 设置标题
	 * 
	 * @param title
	 */
	public void setTitle(String title) {
		mTitle.setText(title);
	}

	/**
	 * 设置右边文字
	 * 
	 * @param rightText
	 */
	public void setRightText(String rightText) {
		mTextRight.setText(rightText);
	}

	/**
	 * 设置右边文字颜色
	 * 
	 * @param color
	 */
	public void setRightTextColor(int color) {
		mTextRight.setTextColor(color);
	}

	/**
	 * 设置右边按钮图片
	 * 
	 * @param icon
	 */
	public void setRightIncon(int icon) {
		rightIcon.setVisibility(View.VISIBLE);
		rightIcon.setBackgroundResource(icon);
	}

	/**
	 * 设置左边按钮图片
	 * 
	 * @param icon
	 */
	public void setLeftIcon(int icon) {
		mBack.setImageResource(icon);
	}

	public void setLeftIconVisible(boolean b) {
		mBack.setVisibility(b ? View.VISIBLE : View.INVISIBLE);
	}

	/**
	 * 回调
	 * 
	 * @param handler
	 */
	public void setTitleBarClickListener(TitleBarHandler handler) {
		this.handler = handler;
	}

}
